using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Scene.Video;
using MicroMsg.Storage;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class ChatVideoItem : BaseChatElement
	{
		private const string TAG = "ChatVideoItem";
		private VideoProgressBar mVideoProgressBar;
		private Image erroImg;
		public MsgTrans mTrans;
		public static DependencyProperty TimeInfoProperty = DependencyProperty.Register("TimeInfo", typeof(string), typeof(ChatVideoItem), new PropertyMetadata("0:00", null));
		public static DependencyProperty LenInfoProperty = DependencyProperty.Register("LenInfo", typeof(string), typeof(ChatVideoItem), new PropertyMetadata("0kb", null));
		public static DependencyProperty ThumbImgProperty = DependencyProperty.Register("ThumbImg", typeof(ImageSource), typeof(ChatVideoItem), new PropertyMetadata(null, null));
		public static DependencyProperty ErrorVisibilityProperty = DependencyProperty.Register("ErrorVisibility", typeof(Visibility), typeof(ChatVideoItem), new PropertyMetadata(1, null));
		public static DependencyProperty ProVisibilityProperty = DependencyProperty.Register("ProVisibility", typeof(Visibility), typeof(ChatVideoItem), new PropertyMetadata(1, null));
		public static DependencyProperty ProgressValueProperty = DependencyProperty.Register("ProgressValue", typeof(double), typeof(ChatVideoItem), new PropertyMetadata(0.0, null));
		private EventWatcher downLoadProgressWatcher;
		public static ImageSource VideoIconNormal = new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/video/video.png") as ImageSource;
		public static ImageSource VideoIconPlay = new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/video/video2.png") as ImageSource;
		public static DependencyProperty VideoIconProperty = DependencyProperty.Register("VideoIcon", typeof(ImageSource), typeof(ChatVideoItem), new PropertyMetadata(ChatVideoItem.VideoIconNormal, null));
		public string TimeInfo
		{
			get
			{
				return (string)base.GetValue(ChatVideoItem.TimeInfoProperty);
			}
			set
			{
				base.SetValue(ChatVideoItem.TimeInfoProperty, value);
			}
		}
		public string LenInfo
		{
			get
			{
				return (string)base.GetValue(ChatVideoItem.LenInfoProperty);
			}
			set
			{
				base.SetValue(ChatVideoItem.LenInfoProperty, value);
			}
		}
		public ImageSource ThumbImg
		{
			get
			{
				return (ImageSource)base.GetValue(ChatVideoItem.ThumbImgProperty);
			}
			set
			{
				base.SetValue(ChatVideoItem.ThumbImgProperty, value);
			}
		}
		public Visibility ErrorVisibility
		{
			get
			{
				return (Visibility)base.GetValue(ChatVideoItem.ErrorVisibilityProperty);
			}
			set
			{
				base.SetValue(ChatVideoItem.ErrorVisibilityProperty, value);
			}
		}
		public Visibility ProVisibility
		{
			get
			{
				return (Visibility)base.GetValue(ChatVideoItem.ProVisibilityProperty);
			}
			set
			{
				base.SetValue(ChatVideoItem.ProVisibilityProperty, value);
			}
		}
		public double ProgressValue
		{
			get
			{
				return (double)base.GetValue(ChatVideoItem.ProgressValueProperty);
			}
			set
			{
				base.SetValue(ChatVideoItem.ProgressValueProperty, value);
			}
		}
		public ImageSource VideoIcon
		{
			get
			{
				return (ImageSource)base.GetValue(ChatVideoItem.VideoIconProperty);
			}
			set
			{
				base.SetValue(ChatVideoItem.VideoIconProperty, value);
			}
		}
		public ChatVideoItem()
		{
			base.add_Loaded(new RoutedEventHandler(this.ChatVideoItem_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.ChatVideoItem_Unloaded));
		}
		private void ChatVideoItem_Unloaded(object sender, RoutedEventArgs e)
		{
			this.ProUnit();
		}
		private void ChatVideoItem_Loaded(object sender, RoutedEventArgs e)
		{
			this.ProInit();
			this.RefreshProgress();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.mVideoProgressBar = (base.GetTemplateChild("progressbar") as VideoProgressBar);
			this.erroImg = (base.GetTemplateChild("erroImg") as Image);
			this.ChatContentCtrl.add_Click(new RoutedEventHandler(this.ContentPanel_Click));
			this.mVideoProgressBar.OnCancelClick += new RoutedEventHandler(this.mVideoProgressBar_OnCancelClick);
			((ClickEffectGrid)base.GetTemplateChild("progressbarLayout")).mClicked += new MMStandardUIEvent(this.mVideoProgressBar_OnCancelClick);
			((ClickEffectGrid)base.GetTemplateChild("erroImgLayout")).mClicked += new MMStandardUIEvent(this.erroImg_MouseLeftButtonDown);
		}
		protected virtual void doResume()
		{
		}
		protected virtual void doCancel()
		{
		}
		protected virtual double getProgressValue()
		{
			return 0.0;
		}
		private void erroImg_MouseLeftButtonDown(object sender, object object_0)
		{
			if (this.chatMsg_0 == null)
			{
				return;
			}
			this.doResume();
			this.RefreshProgress();
		}
		private void mVideoProgressBar_OnCancelClick(object sender, object object_0)
		{
			Log.smethod_5("ChatVideoItem", "mVideoProgressBar_OnCancelClick");
			if (this.mTrans == null)
			{
				return;
			}
			if (this.chatMsg_0 == null)
			{
				return;
			}
			MsgUIStatus status = this.chatMsg_0.GetStatus();
			if (status != MsgUIStatus.Processing)
			{
				return;
			}
			Log.smethod_5("ChatVideoItem", "doCancel");
			this.doCancel();
			this.VideoIcon = ChatVideoItem.VideoIconNormal;
			this.LenInfo = "";
			this.ProVisibility = 1;
			this.ErrorVisibility = 0;
		}
		private void ContentPanel_Click(object sender, RoutedEventArgs e)
		{
			if (this.mclickEvent != null)
			{
				this.mclickEvent(this.chatMsg_0, this);
			}
		}
		public void RefreshUI(MsgTrans trans, string string_0)
		{
			this.mTrans = trans;
			if (this.mTrans == null)
			{
				return;
			}
			this.TimeInfo = string.Format("{0:D2}:{1:D2}", this.mTrans.nDuration / 60, this.mTrans.nDuration % 60);
			BitmapImage bitmapImage = ImageCacheMgr.loadImage(string_0, ImageCacheType.MsgImg);
			if (bitmapImage == null)
			{
				Log.smethod_1("ChatVideoItem", "GetImageFromPath==null,mImgSrc:" + string_0);
				this.ThumbImg = (new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/video/default_thumb.png") as ImageSource);
			}
			else
			{
				this.ThumbImg = bitmapImage;
			}
			this.RefreshProgress();
		}
		public void RefreshProgress()
		{
			if (this.mTrans == null)
			{
				return;
			}
			if (this.chatMsg_0 == null)
			{
				return;
			}
			MsgUIStatus status = this.chatMsg_0.GetStatus();
			if (status == MsgUIStatus.Success)
			{
				this.LenInfo = "";
				this.ProVisibility = 1;
				this.ErrorVisibility = 1;
				this.VideoIcon = ChatVideoItem.VideoIconPlay;
				return;
			}
			if (status == MsgUIStatus.Ready)
			{
				this.VideoIcon = ChatVideoItem.VideoIconNormal;
				this.ProVisibility = 1;
				this.ErrorVisibility = 1;
				if (this.mTrans == null)
				{
					return;
				}
				this.LenInfo = string.Format("{0}k", this.mTrans.nTotalDataLen / 1024);
				return;
			}
			else
			{
				if (status == MsgUIStatus.Processing)
				{
					this.VideoIcon = ChatVideoItem.VideoIconNormal;
					this.LenInfo = "";
					this.ProVisibility = 0;
					this.ErrorVisibility = 1;
					this.ProgressValue = this.getProgressValue();
					return;
				}
				this.VideoIcon = ChatVideoItem.VideoIconNormal;
				this.LenInfo = "";
				this.ProVisibility = 1;
				this.ErrorVisibility = 0;
				return;
			}
		}
		private void ProInit()
		{
			this.downLoadProgressWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.on_ProgressChanged));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_DOWNLOADVIDEO_PROGRESS, this.downLoadProgressWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_UPLOADVIDEO_PROGRESS, this.downLoadProgressWatcher);
		}
		private void ProUnit()
		{
			if (this.downLoadProgressWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_DOWNLOADVIDEO_PROGRESS, this.downLoadProgressWatcher);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_UPLOADVIDEO_PROGRESS, this.downLoadProgressWatcher);
				this.downLoadProgressWatcher = null;
			}
		}
		private void on_ProgressChanged(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			Log.smethod_4("ChatVideoItem", "on_LoadProgress_EventHandler");
			if (this.chatMsg_0 != null && evtArgs.mObject != null)
			{
				DownloadVideoContext.ProgressInfo progressInfo = evtArgs.mObject as DownloadVideoContext.ProgressInfo;
				if (progressInfo != null && progressInfo.msgSvrId == this.chatMsg_0.nMsgSvrID)
				{
					this.RefreshProgress();
				}
				UploadVideoContext.ProgressInfo progressInfo2 = evtArgs.mObject as UploadVideoContext.ProgressInfo;
				if (progressInfo2 != null && progressInfo2.clientMsgId == this.chatMsg_0.strClientMsgId)
				{
					this.RefreshProgress();
				}
				return;
			}
		}
	}
}
